package com.woniuxy.usercoachassemble.exception;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.support.spring.FastJsonJsonView;

@Component
public class GolbalExceptionResovler implements HandlerExceptionResolver{

	@Override
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
			Exception ex) {
		ex.printStackTrace();
		FastJsonJsonView view = new FastJsonJsonView();
		response.setStatus(500);
		HashMap<String,Object> map = new HashMap<String,Object>();
		map.put("code",500);
	 if(ex instanceof TimeScopeException){
			map.put("message", ex.getMessage());
		}else if(ex instanceof StatusAleadyChangeException){
			map.put("message", ex.getMessage());
		}else if(ex instanceof NotChangeMessage){
			map.put("message", ex.getMessage());
		}
		else {
			map.put("message", "服务器异常");
		}
		view.setAttributesMap(map);
		view.setAttributesMap(map);
		return new ModelAndView(view);
	}

}
